package com.boruiimage.demo.service;

import com.boruiimage.demo.doMain.Result;
import com.boruiimage.demo.doMain.user;
import com.boruiimage.demo.mapperDao.registerMapperDao;
import com.boruiimage.demo.repository.registerDao;
import com.boruiimage.demo.utils.AES;
import com.boruiimage.demo.utils.ALiMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class registerService implements registerDao {
    @Autowired
   private  registerMapperDao rmd;
    //注册
    @Override
    public int addUser(String userName, String userPassword, String code, HttpServletRequest request) {
        //第一次获取手机时是在点击获取验证码传递手机号
        //查询该电话是否被注册过
        HttpSession session=request.getSession();
        user user=rmd.SeletctByuserName(AES.encrypt(userName));
        if(""==code){
            if(user!=null){
                return 2;//该手机号已被注册
            }else{
                //通过手机号获取验证码
                 String rodum =ALiMessage.show(userName);//这个地方通过获取短信验证码的工具类生成
                System.out.println(rodum+"rodum");
                session.setAttribute(userName,rodum);//生成之后存到session里面
            }
        }
        if(""!=code){
            if(user!=null){
                return 2;//该手机号已被注册
            }
            System.out.println(session.getAttribute(userName)+"session");
            //点击注册时获取验证码
            if(code.equals(session.getAttribute(userName))){//如果的到的验证码一致
                int adduser=rmd.addUser(AES.encrypt(userName),AES.encrypt(userPassword));
                if(adduser>0){
                    return 0;
                }
                return 1;
            }else{
                return 3;//返回失败
            }
        }
        return 4;
    }
   //登录
    @Override
    public Result selectUSer(String userName, String userPassword) {
        Result rs=new Result();
        user user=rmd.SelectUser(AES.encrypt(userName),AES.encrypt(userPassword));
        if(user!=null){
            rs.setMsg("登录成功");
        }else
        {
            rs.setMsg("用户名或密码错误");
        }
        return rs;
    }
    //修改密码
    @Override
    public Result upUser(String userName, String userPassword, String code, HttpServletRequest request) {
        //当点击获取验证码时获取第一次获取手机号
        String redom=null;
        Result rs=new Result();
        HttpSession session=request.getSession();
        if(""==code){
            user user=rmd.SeletctByuserName(AES.encrypt(userName));
            if(user==null){
                rs.setMsg("该用户不存在");
            }else {
                redom =ALiMessage.show(userName);//给手机传递验证码;
                session.setAttribute(userName,redom);//存入session中
            }
        }
        if(""!=code){
            if(code.equals(session.getAttribute(userName))){
               int upUser=rmd.upUser(AES.encrypt(userName),AES.encrypt(userPassword));
               if(upUser>0){
                   rs.setMsg("更改密码成功");
               }else{
                   rs.setMsg("更改密码失败");
               }
            }else {
                rs.setMsg("验证码不一致");
            }
        }
        return rs;
    }
   //APP端登录
    @Override
    public Result selectUser(String userName, String passwprd) {
        Result rs=new Result();
        user user =rmd.selectUser(userName,passwprd);
        if(user!=null) {
            rs.setMsg("成功");
        }else{
            rs.setMsg("失败");
        }
        return rs;
    }
    //APP端检测是否被注册
    @Override
    public Result SeletctByuserNameS(String userName) {
        user user =rmd.SeletctByuserName(AES.encrypt(userName));
        Result rs=new Result();
        if(user!=null) {
            rs.setMsg("失败");
        }else{
            rs.setMsg("成功");
        }
        return rs;
    }

    @Override
    public int addUserS(String userName, String userPassword) {
        return rmd.addUser(userName,userPassword);
    }

    @Override
    public int addmess(String connect, String code) {
        return rmd.addmess(connect,code);
    }

    @Override
    public String Select(String connect) {
        return rmd.Select(connect);
    }

    @Override
    public int de() {
        return rmd.de();
    }
   //APP端修改密码
    @Override
    public int upUsers(String userName, String userPassword) {
        return rmd.upUser(userName,userPassword);
    }
    //删除数据通过connect
    @Override
    public int delByconnect(String connect) {
        return rmd.delByconnect(connect);
    }


}
